#include "main.h"
#include "uart.h"

extern int distance_arr[3]; //存放与3基站的距离



//将n个ascii转int值
static int read_ascii_num(int ascii_num, uint8_t* ascii_ptr, int* num){
	
	uint8_t* ascii ;
	ascii = ascii_ptr;
	
	*num = 0;
	
	
	if(ascii_num > 4){
		if((*ascii <'0') || (*ascii >'9'))return ERR;
		*num += ((*ascii-'0') * 10000);
		ascii++;
	}
	if(ascii_num > 3){
		if((*ascii <'0') || (*ascii >'9'))return ERR;
		*num += ((*ascii-'0') * 1000);
		ascii++;
	}
	if(ascii_num > 2){
		if((*ascii <'0') || (*ascii >'9'))return ERR;
		*num += ((*ascii-'0') * 100);
		ascii++;
	}
	if(ascii_num > 1){
		if((*ascii <'0') || (*ascii >'9'))return ERR;
		*num += ((*ascii-'0') * 10);
		ascii++;
	}
	if(ascii_num > 0){
		if((*ascii <'0') || (*ascii >'9'))return ERR;
		*num += ((*ascii-'0') * 1);
	}
	
	return SUCCESS;
	
}




//读取fd的数值内容并转数值格式到全局数组变量
static int read_dist(int fd){
	uint8_t distance1_ascii[3]; //存放与基站1的距离ascii值
	uint8_t distance2_ascii[3];
	uint8_t distance3_ascii[3];
	//TODO write in int? will be num?
	read(fd, &distance1_ascii, 3);
	read(fd, &distance2_ascii, 3);
	read(fd, &distance3_ascii, 3);

	if(read_ascii_num(3, distance1_ascii, &distance_arr[0]) == ERR)return DIST_ERR; //写入基站距离
	if(read_ascii_num(3, distance2_ascii, &distance_arr[1]) == ERR)return DIST_ERR;
	if(read_ascii_num(3, distance3_ascii, &distance_arr[2]) == ERR)return DIST_ERR;
	
	return SUCCESS;
}
//TODO
int read_temp(int fd){
	return SUCCESS;
}
int read_evrm(int fd){
	return SUCCESS;
}
int read_acce(int fd){
	return SUCCESS;
}





int recv_respond(int fd, uint8_t type){
	uint8_t resp_len = 0;
	uint8_t resp_type = 0;
	int res = 0;

	//通过type确定将等待的respond长度
	switch(type){
		case DIST_CMD_TYPE	: resp_len = DIST_RESP_LEN;break;
		case TEMP_CMD_TYPE	: resp_len = TEMP_RESP_LEN;break;
		case EVRM_CMD_TYPE	: resp_len = EVRM_RESP_LEN;break;
		case ACCE_CMD_TYPE	: resp_len = ACCE_RESP_LEN;break;
		default	:	return TYPE_ERR;
	}
	while(serialDataAvail(fd) < resp_len){
		usleep(10000);	//10ms
	}
	//判断协议
	while(serialGetchar(fd)!='A');
	if(serialGetchar(fd)!='A')return RESP_ERR;
	//对resp的type进行分类执行
	resp_type = serialGetchar(fd);
	if(resp_type == type){	//收到有结果resp
		switch(type){
			case DIST_CMD_TYPE	: res = read_dist(fd);break;//从resp读取dist数值并写入全局变量
			case TEMP_CMD_TYPE	: res = read_temp(fd);break;
			case EVRM_CMD_TYPE	: res = read_evrm(fd);break;
			case ACCE_CMD_TYPE	: res = read_acce(fd);break;
			default	: return TYPE_ERR;
			
		}
		if(res != SUCCESS)return res; //读取失败
		
		if(serialGetchar(fd)!='D')return RESP_ERR;
		if(serialGetchar(fd)!='D')return RESP_ERR;
		return SUCCESS;
	}
	else if(resp_type == (type + 0x20)){	//收到无结果resp
	
		return TYPE_ERR;
	}
	//TODO add?
	serialFlush(fd);
	return TYPE_ERR;
}






// int main()
// {
        
		
        // int n_read;
        // char r_buff[128] = {'\0'};
        // int recv= 0;

        // if(-1 == wiringPiSetup()){
                // printf("Set up fail\n");
                // exit(-1);
        // }
                // // 串口打开成功返回文件描述符
        // fd = serialOpen("/dev/ttyAMA0",115200);

        // if(-1 == fd){
                // printf("Open serial fail\n");
                // exit(-1);
        // }
        // printf("start\r\n");
        // serialFlush(fd);

        // while(1){
            // while(serialDataAvail(fd) > 13 )
			// {
				// while(serialGetchar(fd)!='A');
				// if(serialGetchar(fd)=='A'){
					// if(-1 == readcmdtype())break;	
					// printf("%c%c%c\r\n",distance1_ascii[0],distance1_ascii[1],distance1_ascii[2]);
				// }    
			// }				
        // }

        // return 0;
// }





